package cn.lyh.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.lyh.edu.domain.EduBanner;
import cn.lyh.exception.BadRequestException;
import cn.lyh.edu.domain.EduTeacher;
import cn.lyh.edu.domain.EduLesson;
import cn.lyh.edu.domain.EduLessonType;
import cn.lyh.edu.repository.EduTeacherRepository;
import cn.lyh.edu.repository.EduLessonRepository;
import cn.lyh.edu.repository.EduLessonTypeRepository;
import cn.lyh.edu.service.IEduLessonService;
import cn.lyh.edu.service.dto.LessonQueryCriteria;
import cn.lyh.utils.PageUtil;
import cn.lyh.utils.QueryHelp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**功能描述：课程信息业务接口实现类

 */
@Service
@Transactional(readOnly = true)
public class EduLessonServiceImpl implements IEduLessonService {

    private final EduLessonRepository eduLessonRepository;

    private final EduTeacherRepository eduTeacherRepository;

    private final EduLessonTypeRepository eduLessonTypeRepository;

    public EduLessonServiceImpl(EduLessonRepository eduLessonRepository, EduTeacherRepository eduTeacherRepository, EduLessonTypeRepository eduLessonTypeRepository) {
        this.eduLessonRepository = eduLessonRepository;
        this.eduTeacherRepository = eduTeacherRepository;
        this.eduLessonTypeRepository = eduLessonTypeRepository;
    }

    /**
     * 获取课程列表数据
     * @param queryCriteria
     * @param pageable
     * @return
     */
    @Override
    public Object getList(LessonQueryCriteria queryCriteria, Pageable pageable) {
        Page<EduLesson> page = eduLessonRepository.findAll((root, query, criteriaBuilder)-> QueryHelp.getPredicate(root,queryCriteria,criteriaBuilder),pageable);
        setTeacherAndTypeToLesson(page.stream().toList());
        return PageUtil.toPage(page);
    }

    /**
     * 添加教师对象和课程类型对象到课程列表
     * @param eduLessons
     */
    private List<EduLesson> setTeacherAndTypeToLesson(List<EduLesson> eduLessons) {
        for (EduLesson r: eduLessons){
            // 处理教师
            if(r.getTeacherId()!=null&&r.getTeacherId()!=0){
                EduTeacher eduTeacher = eduTeacherRepository.findById(r.getTeacherId()).orElseGet(EduTeacher::new);
                if (StringUtils.isNotBlank(eduTeacher.getTeacherName())){
                    r.setEduTeacher(eduTeacher);
                }
            }
            // 课程类型
            if(r.getLessonTypeId()!=null&&r.getLessonTypeId()!=0){
                EduLessonType eduLessonType = eduLessonTypeRepository.findById(r.getLessonTypeId()).orElseGet(EduLessonType::new);
                if(StringUtils.isNotBlank(eduLessonType.getTypeName())){
                    r.setEduLessonType(eduLessonType);
                }
            }

        }
        return eduLessons;
    }

    /**
     * 添加课程信息
     * @param eduLesson
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addLesson(EduLesson eduLesson) {
        // 根据课程号查询记录
        EduLesson dbEduLesson = eduLessonRepository.findByLessonNumber(eduLesson.getLessonNumber());
        if(dbEduLesson!=null){
            throw new BadRequestException("添加失败，课程号已经存在！");
        }
        else {
//            // 课程状态，0表示空闲
//            eduLesson.setLessonStatus(0);
            eduLessonRepository.save(eduLesson);
        }

    }

    /**
     * 根据ID获取详情信息
     * @param id
     * @return
     */
    @Override
    public EduLesson getById(Long id) {
        return eduLessonRepository.findById(id).orElseGet(EduLesson::new);
    }

    /**
     * 更新课程信息
     * @param eduLesson
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editLesson(EduLesson eduLesson) {
        // 根据课程号获取信息
        EduLesson dbEduLesson = eduLessonRepository.findByLessonNumber(eduLesson.getLessonNumber());
        if(dbEduLesson!=null&&!dbEduLesson.getId().equals(eduLesson.getId())){
            throw new BadRequestException("更新失败，课程号已经存在！");
        }else {
            dbEduLesson = eduLessonRepository.getReferenceById(eduLesson.getId());
            BeanUtil.copyProperties(eduLesson,dbEduLesson, CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
            eduLessonRepository.save(dbEduLesson);
        }
    }

    /**
     * 删除课程信息
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) {
        eduLessonRepository.deleteById(id);
    }


    /**
     * 根据楼层ID获取房间
     * @param id
     *       楼层ID
     * @param num
     *       房间数量
     * @return
     */
    @Override
    public List<EduLesson> getLessonListByTeacherId(Long id, int num) {
        return eduLessonRepository.getLessonListByTeacherId(id,num);
    }

    /**
     * 获取所有课程
     * @return
     */
    @Override
    public List<EduLesson> getAll() {
        return eduLessonRepository.findAll();
    }

}
